#!/usr/bin/python
# -*- coding: utf-8 -*-


import tornado
import tornado.ioloop
import tornado.web
from tornado.escape import json_encode
import base64




class Handler(tornado.web.RequestHandler):

    def post(self):
        result = {}
        imagebase64 = self.get_argument("image", default="")
        #print image_url
        imgdata = base64.b64decode(imagebase64)
        with open('1.png','wb') as f:
            f.write(imgdata)

        if not imagebase64:
            result["errcode "] = 404
            result["msg"] = "no image url"
        else:
            result["errcode"] = 200
            result["msg"] = "get"
        self.write(json_encode(result))



class ImageServer(object):

    def __init__(self, port):
        self.port = port

    def process(self):
        app = tornado.web.Application([(r"/ocr?", Handler)], )
        app.listen(self.port)
        tornado.ioloop.IOLoop.current().start()

if __name__ == "__main__":
    server_port = "12381"
    server = ImageServer(server_port)
    print ("begin server")
    server.process()
